home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / hd / rdbmount.lha / RDBMount / RDBMount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-29  |  8.8 KB  |  301 lines

  1. /*
  2.  *    Copyright © 1991 by S.R.
  3.  *
  4.  *      Created: 28 Mar 1993
  5.  *      Modified:    17 Aug 1994    10:13:49
  6.  *
  7.  * Make>> SC <file>.c
  8.  * Make>> SLink Lib:Entry.o <file>.o LIB Lib:String.lib MAP <file>.map F,H,L,O,S,X NOICON VERBOSE SmallCode SmallData BATCH
  9.  * Ma ke>> SLink Lib:Entry.o <file>.o LIB Lib:Startup.lib Lib:String.lib MAP <file>.map F,H,L,O,S,X NOICON VERBOSE SmallCode SmallData BATCH
  10.  */
  11.  
  12. #include <Main.h>
  13. #include <StringLib.h>
  14.  
  15. #include <Libraries/HardBlocks.h>
  16. /*#include <pragmas/HardBlocks_pragmas.h> */
  17.  
  18. #define MAXSTRINGLEN 255/2
  19. #define MAXNUMARGS 64
  20. #define MAXDEFAULTLEN 512/2
  21.  
  22. STRPTR Template = "DosDev/M/A,ExecDev,UnitNr/N,FS/K";
  23.  
  24. #define ARG_DosDevA    0
  25. #define ARG_ExecDev    1
  26. #define ARG_UnitNr    2
  27. #define ARG_FS            3
  28. #define ARG_ENDARG    4
  29. STRPTR CliHelp = "RDBMount V1.1  "__DATE__" © Sylvain ROUGIER.\n\
  30. Usage: RDBMount <DosDevName> [ExecDevName] [ExecUnitNr] [ FS FileSystemName]\n\
  31. Default: ExecDevName = scsi.device ExecUnitNr = 0 or read Env \"RDBMount.DefOpts\"\n\
  32. Use to mount DOS Dev from the RigidDiskBlock information stored on (HD) drive\n";
  33.  
  34. const STRPTR VersTag = "$VER: RDBMount 1.1 (17.08.94) "__DATE__ " " __TIME__ " © Sylvain ROUGIER";
  35.  
  36. struct DosLibrary *DOSBase;
  37. struct Library *UtilityBase;
  38. struct Library *ExpansionBase;
  39. struct Library *HardBlocksBase;
  40.  
  41. char * CStrToBStr( char CStr[])
  42. {
  43.     ULONG l;
  44.     char *BStr;
  45.  
  46.     l = strlen( CStr);
  47.     if ( BStr = AllocMem( l + 1 + 1, MEMF_PUBLIC))
  48.         {
  49.             strcpy( &BStr[ 1], CStr);
  50.             BStr[ 0] = l;
  51.             return BStr;
  52.         }
  53.  
  54.     return NULL;
  55. }
  56.  
  57. char *B2CStr(UBYTE * BStr, char Buff[])
  58. {
  59.     UBYTE i;
  60.  
  61.     for (i = 0; i < *BStr; i++)
  62.     {
  63.         Buff[i] = (BStr + 1)[i];
  64.     }
  65.     Buff[i] = '\0';
  66.  
  67.     return Buff;
  68. }
  69.  
  70. BOOL IsMounted(char DosName[])
  71. {
  72.     struct DosList *DList;
  73.     BOOL Found = FALSE;
  74.  
  75.     DList = LockDosList(LDF_DEVICES | LDF_READ);
  76.     Found = (BOOL) FindDosEntry(DList, DosName, LDF_DEVICES);
  77.     UnLockDosList(LDF_DEVICES | LDF_READ);
  78.  
  79.     return Found;
  80. }
  81.  
  82. ULONG __asm Main(register __a0 APTR ArgV[], register __a1 struct WBStartup * WBenchMsg);
  83. ULONG __asm __Main(register __d2 ULONG CmdLen, register __a2 STRPTR CmdPtr)
  84. {
  85.     struct Process *pp;
  86.     ULONG RC = 1001;    /* assumle the worst */
  87.  
  88.     pp = (struct Process *)FindTask(0L);
  89.  
  90.     if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 34L))
  91.     {
  92.         if (DOSBase->dl_lib.lib_Version > 36)
  93.         {
  94.             APTR ArgV[MAXNUMARGS];
  95.             struct RDArgs *RA;
  96.             struct RDArgs *DefaultRDArgs;
  97.  
  98.             UtilityBase = DOSBase->dl_UtilityBase;
  99.             if (RA = AllocDosObject(DOS_RDARGS, NULL))
  100.             {
  101.                 memset(ArgV, 0, MAXNUMARGS * sizeof (APTR));
  102.                 RA->RDA_ExtHelp = CliHelp;
  103.                 /* first read default */
  104.                 /* getting the default string */
  105.                 if (DefaultRDArgs = AllocDosObject(DOS_RDARGS, NULL))
  106.                 {
  107.                     UBYTE Default[MAXDEFAULTLEN] = "DosDev Dummy ExecDev scsi.device UnitNr 0";
  108.  
  109.                     if ((DefaultRDArgs->RDA_Source.CS_Length = GetVar("RDBMount.DefOpts", Default, MAXDEFAULTLEN - 1, 0L)) > 0)
  110.                         ;
  111.                     else
  112.                         DefaultRDArgs->RDA_Source.CS_Length = strlen(Default);
  113.                     {
  114.                         /* let ReadArgs() allocate neccesary buffer sinc I use diferrent RDArgs for Default and CLI opts */
  115.  
  116.                         /* Ok I succefuly read a Default Option string now parse it */
  117.                         Default[DefaultRDArgs->RDA_Source.CS_Length++] = '\n';    /* this is need by ReadArgs() */
  118.                         Default[DefaultRDArgs->RDA_Source.CS_Length] = '\0';    /* this is need by ReadArgs() */
  119.                         DefaultRDArgs->RDA_Source.CS_Buffer = Default;
  120.                         DefaultRDArgs->RDA_Source.CS_CurChr = 0;
  121.                         DefaultRDArgs->RDA_Flags = RDAF_NOPROMPT;
  122.                         if (!ReadArgs(Template, (long *)ArgV, DefaultRDArgs))
  123.                         {
  124.                             PutStr("Error in Default:");
  125.                             PrintFault(IoErr(), NULL);
  126.                             memset(ArgV, 0, MAXNUMARGS * sizeof (APTR));
  127.                         }
  128.                         DefaultRDArgs->RDA_Source.CS_Buffer = NULL;    /* Now ReadArgs() from Command line */
  129.                         /* the ArgV is leaving unchanged: the second ReadArgs() take actual value as default */
  130.                     }
  131.                 }
  132.                 /* Let ReadArgs() allocate necessay buffer rather using stack space */
  133.                 if (ReadArgs(Template, (long *)ArgV, RA))
  134.                 {
  135.                     RC = Main(ArgV, NULL);
  136.                 }
  137.                 else
  138.                 {
  139.                     PrintFault(IoErr(), NULL);
  140.                     RC = 20;
  141.                 }
  142.                 FreeDosObject(DOS_RDARGS, RA);
  143.                 /* Free RDargs now I don't need the option value */
  144.                 if (DefaultRDArgs)
  145.                     FreeDosObject(DOS_RDARGS, DefaultRDArgs);
  146.             }
  147.         }
  148.         else
  149.         {
  150.             Write(pp->pr_COS, "You need KickStart 2.0+\n", 24L);
  151.             RC = 1000;
  152.         }
  153.         CloseLibrary((struct Library *)DOSBase);
  154.     }
  155.  
  156.     return RC;
  157. }
  158.  
  159. struct FileSysEntry *GetFileSys( ULONG DosType)
  160. {
  161.     struct FileSysResource *FileSysResource;
  162.     struct FileSysEntry *i;
  163.  
  164.     if ( FileSysResource = OpenResource( "FileSystem.resource"))
  165.     {
  166.         for ( i = ( struct FileSysEntry *)FileSysResource->fsr_FileSysEntries.lh_Head; i->fse_Node.ln_Succ; i = ( struct FileSysEntry *)i->fse_Node.ln_Succ)
  167.         {
  168.             if ( i->fse_DosType == DosType)
  169.                 return i;
  170.         }
  171.     }
  172.  
  173.     return NULL;
  174. }
  175.  
  176. ULONG __asm Main(register __a0 APTR ArgV[], register __a1 struct WBStartup * WBenchMsg)
  177. {
  178.     ULONG RC = 20;        /* default :Don't find desired Partition */
  179.  
  180.     ExpansionBase = OpenLibrary("expansion.library", 37L);
  181.  
  182.     if (HardBlocksBase = OpenLibrary(HardBlocksName, HardBlocksVersion))
  183.     {
  184.         struct RigidDiskBlock rdb;
  185.  
  186.         if (!(RC = LoadHardBlocks(&rdb, ArgV[ARG_ExecDev], *((ULONG *) ArgV[ARG_UnitNr]))))
  187.         {
  188.             #define MAXMOUNT 10
  189.             ULONG parmPkt[MAXMOUNT][21 + 10];    /* WARN: this is a lenthg which can change */
  190.             char DevNames[MAXMOUNT][20];    /* mem string don't be lost until the mount */
  191.             UBYTE MountCnt = 0;
  192.             UBYTE i, j;
  193.             char *Str;
  194.  
  195.             for (i = 0, Str = ((char **)ArgV[ARG_DosDevA])[0]; Str && (i < MAXMOUNT); i++, Str = ((char **)ArgV[ARG_DosDevA])[i])
  196.             {
  197.                 BOOL Found = FALSE;
  198.                 struct PartitionBlock *PB;
  199.                 char Buff[MAXSTRINGLEN];
  200.                 char DosDevName[MAXSTRINGLEN];
  201.  
  202.                 strcpy(DosDevName, Str);
  203.                 {
  204.                     UBYTE l;
  205.  
  206.                     if (DosDevName[l = (strlen(DosDevName) - 1)] == ':')
  207.                         DosDevName[l] = '\0';
  208.                 }
  209.                 if (!IsMounted(DosDevName))
  210.                 {
  211.                     for (PB = (struct PartitionBlock *)rdb.rdb_PartitionList; PB && !Found; PB = (struct PartitionBlock *)PB->pb_Next)
  212.                     {
  213.                         B2CStr(PB->pb_DriveName, Buff);
  214.                         //Printf( "Dos Name:\"%s\"\n", Buff);
  215.                         //Printf("pb:%lx, next:%lx\n", PB, PB->pb_Next);
  216.                         if (!Strnicmp(DosDevName, Buff, MAXSTRINGLEN))
  217.                         {
  218.                             strcpy(DevNames[MountCnt], Buff);
  219.                             parmPkt[MountCnt][0] = (ULONG) DevNames[MountCnt];
  220.                             parmPkt[MountCnt][1] = (ULONG) ArgV[ARG_ExecDev];
  221.                             parmPkt[MountCnt][2] = *((ULONG *) ArgV[ARG_UnitNr]);    /* unit number */
  222.                             parmPkt[MountCnt][3] = 0;    /* OpenDevice flags */
  223.                             CopyMem(PB->pb_Environment, &parmPkt[MountCnt][4], sizeof (PB->pb_Environment));
  224.                             Found = TRUE;
  225.                             MountCnt++;
  226.                         }
  227.                     }
  228.                     if (!Found)
  229.                         Printf("\"%s:\" not found.\n", DosDevName);
  230.                 }
  231.                 else
  232.                 {
  233.                     Printf("\"%s:\" alredy mounted.\n", DosDevName);
  234.                 }
  235.             }
  236.             for (j = 0; j < MountCnt; j++)
  237.             {
  238.                 struct DeviceNode *DeviceNode;
  239.  
  240.                 if (DeviceNode = MakeDosNode(parmPkt[j]))
  241.                 {
  242.                     struct FileSysEntry *FileSysEntry;
  243.  
  244.                     if ( FileSysEntry = GetFileSys( parmPkt[ j][ 3 +17]))
  245.                     {
  246.                         if ( FileSysEntry->fse_PatchFlags & 0x00000001)
  247.                             DeviceNode->dn_Type = FileSysEntry->fse_Type;
  248.                         if ( FileSysEntry->fse_PatchFlags & 0x00000002)
  249.                             DeviceNode->dn_Task = ( struct MsgPort *)FileSysEntry->fse_Task;
  250.                         if ( FileSysEntry->fse_PatchFlags & 0x00000004)
  251.                             DeviceNode->dn_Lock = FileSysEntry->fse_Lock;
  252.                         if ( FileSysEntry->fse_PatchFlags & 0x00000008)
  253.                             DeviceNode->dn_Handler = FileSysEntry->fse_Handler;
  254.                         if ( FileSysEntry->fse_PatchFlags & 0x00000010)
  255.                             DeviceNode->dn_StackSize = FileSysEntry->fse_StackSize;
  256.                         if ( FileSysEntry->fse_PatchFlags & 0x00000020)
  257.                             DeviceNode->dn_Priority = FileSysEntry->fse_Priority;
  258.                         if ( FileSysEntry->fse_PatchFlags & 0x00000040)
  259.                             DeviceNode->dn_Startup = FileSysEntry->fse_Startup;
  260.                         if ( FileSysEntry->fse_PatchFlags & 0x00000080)
  261.                             DeviceNode->dn_SegList = FileSysEntry->fse_SegList;
  262.                         if ( FileSysEntry->fse_PatchFlags & 0x00000100)
  263.                             DeviceNode->dn_GlobalVec = FileSysEntry->fse_GlobalVec;
  264.                         /*{
  265.                             struct FileSysStartupMsg *fssm;
  266.                             struct DosEnvec *de;
  267.                             fssm = BADDR( DeviceNode->dn_Startup);
  268.                             de = BADDR( fssm->fssm_Environ);
  269.                             Printf( "toto");
  270.                         }*/
  271.                     }
  272.                     else
  273.                         Printf( "Can't find Segment for DosType:%l08x in FileSystem.resource\n", parmPkt[ j][ 3 +17]); 
  274.                     if ( ArgV[ ARG_FS])
  275.                     {
  276.                         DeviceNode->dn_Handler = MKBADDR( CStrToBStr( ArgV[ ARG_FS]));
  277.                         DeviceNode->dn_SegList = NULL;
  278.                         DeviceNode->dn_GlobalVec = -1;
  279.                     }
  280.                     
  281.                     if (!AddDosNode(-128, ADNF_STARTPROC, DeviceNode))
  282.                     {
  283.                         Printf("\"%s\" can't be Mounted\n", parmPkt[j][0]);
  284.                     }
  285.  
  286.                 }
  287.             }
  288.             FreeHardBlocks(&rdb);
  289.         }
  290.         else
  291.             Printf("Unable to Load RDB from \"%s\" %ld\n", ArgV[ARG_ExecDev], *((ULONG *) ArgV[ARG_UnitNr]));
  292.         CloseLibrary(HardBlocksBase);
  293.     }
  294.     else
  295.         Printf("Unable to open hardblocks.library V1+\n");
  296.  
  297.     CloseLibrary(ExpansionBase);
  298.  
  299.     return RC;
  300. }
  301.